package item;

import game.GameObject;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;


public class BaseItem implements GameItem, java.io.Serializable{

	protected ItemTemplate template;
	protected long id;
	protected int instanceId;
	protected long createTimeStamp;
	protected boolean bound;
	protected GameItemObject object;
	protected int obsoleteTime;
	
	public BaseItem(ItemTemplate template, long id, int instanceId,long createTimeStamp){
		this.template = template;
		this.id = id;
		this.instanceId = instanceId;
		this.createTimeStamp = createTimeStamp;
		if(template.getBindType()==ItemTemplate.BIND_REWARD){
			setBound(true);
		}
	}
	
	public BaseItem(ItemTemplate template, long id, int instanceId){
		this(template,id,instanceId,System.currentTimeMillis());
	}

	public long getId() {
		return this.id;
	}	
	

	public int getInstanceId() {
		return instanceId;
	}

	public String getName() {
		return template.getName();
	}

	/*public GameObjectRef ref() {
		return null;
	}*/
	
	public void setBound(boolean bound){
		this.bound = bound;
	}

	@Override
	public long getCreateTime() {
		return createTimeStamp;
	}

	@Override
	public String getDesc() {
		if(null != object){
			String desc = object.getDesc();
			if(null != desc){
				return desc;
			}
		}
		return template.getDesc();
	}

	@Override
	public ItemTemplate getTemplate() {
		return template;
	}

	@Override
	public int getPrice() {
		return template.getPrice();
	}

	@Override
	public int getQuality() {
		return template.getQuality();
	}

	@Override
	public boolean isBound() {
		return bound;
	}

	public byte[] toClientBytes(){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			//dos.write(template.toClientBytes());
			dos.writeInt(this.template.getId());
			dos.writeInt(instanceId);
			dos.writeByte(bound?1:0); 
			
		} catch (IOException e) {
		}
		return baos.toByteArray();
	}
	
	public static final byte[] toClientBytes(ItemTemplate template){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			//dos.write(template.toClientBytes());
			dos.writeInt(template.getId());
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return baos.toByteArray();		
	}
	
	public boolean equals(Object obj) {
		BaseItem bi = (BaseItem)obj;
		if(bi.instanceId == this.instanceId &&
				bi.getTemplate() == this.getTemplate()) {
			return true;
		}
			
		return false;
	}
	
	@Override
	public GameItemObject getGameItemObject(){
		return object;
	}
	
	public int getType(){
		return GameObject.TYPE_ITEM;
	}
	
	@Override
	public BaseItem clone(){
		BaseItem ret = new BaseItem(template,id,instanceId,createTimeStamp);
		if(object!=null){
			ret.object = object.clone();
		}
		ret.obsoleteTime = obsoleteTime;
		return ret;
	}

	public void setGameItemObject(GameItemObject object) {
		this.object = object;
	}
	
	public int getObsoleteTime(){
		return this.obsoleteTime;
	}
	
	public void setObsoleteTime(int obsoleteTime){
		this.obsoleteTime = obsoleteTime;
	}
}
